package com.example.online.music.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.online.music.common.R;
import com.example.online.music.model.request.AdminRequest;
import com.example.online.music.security.model.Result;
import com.example.online.music.security.utils.JwtUtils;
import com.example.online.music.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台管理的相关事宜
 */
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    // 判断是否登录成功
    @PostMapping("/admin/login/status")
    public R loginStatus(@RequestBody AdminRequest adminRequest, HttpSession session) {
        R result = adminService.verityPasswd(adminRequest, session);
        //登录成功的话，返回中包含token信息
        if(result.getSuccess()){
            String tokenStr = JwtUtils.generateToken(adminRequest.getUsername());
            Result token = new Result();
            token.setAuthorization(tokenStr);
            result.setData(token);
        }
        return result;
    }
}
